package com.zenithmind.version.controller.handler;

import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.version.pojo.dto.RepositoryCreateDTO;
import com.zenithmind.version.pojo.dto.RepositoryStatisticsDTO;
import com.zenithmind.version.pojo.vo.RepositoryVO;
import com.zenithmind.version.service.RepositoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 仓库请求处理器 - 遵循单一职责原则，专门处理仓库相关的请求逻辑
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Component
@RequiredArgsConstructor
public class RepositoryRequestHandler extends BaseRequestHandler {

    private final RepositoryService repositoryService;

    /**
     * 处理创建仓库请求
     * 
     * @param createDTO 创建DTO
     * @return 处理结果
     */
    public Result<RepositoryVO> handleCreateRepository(RepositoryCreateDTO createDTO) {
        return executeOperation(
            () -> repositoryService.createRepository(createDTO),
            "创建仓库"
        );
    }

    /**
     * 处理分页查询仓库请求
     * 
     * @param current 当前页
     * @param size 页大小
     * @param ownerId 所有者ID
     * @param type 仓库类型
     * @param status 仓库状态
     * @param keyword 关键词
     * @return 处理结果
     */
    public Result<PageResult<RepositoryVO>> handleGetRepositoryPage(Long current, Long size,
                                                                   String ownerId, Integer type,
                                                                   Integer status, String keyword) {
        return executeOperation(
            () -> repositoryService.getRepositoryPage(current, size, ownerId, type, status, keyword),
            "分页查询仓库"
        );
    }

    /**
     * 处理获取仓库详情请求
     *
     * @param id 仓库ID
     * @return 处理结果
     */
    public Result<RepositoryVO> handleGetRepositoryById(String id) {
        return executeOperation(
            () -> repositoryService.getRepositoryById(id),
            "获取仓库详情"
        );
    }

    /**
     * 处理根据名称和所有者查询仓库请求
     *
     * @param repositoryName 仓库名称
     * @param ownerName 所有者名称
     * @return 处理结果
     */
    public Result<RepositoryVO> handleGetRepositoryByNameAndOwner(String repositoryName, String ownerName) {
        return executeOperation(
            () -> repositoryService.getRepositoryByNameAndOwner(repositoryName, ownerName),
            "根据名称和所有者查询仓库"
        );
    }

    /**
     * 处理更新仓库请求
     * 
     * @param id 仓库ID
     * @param updateDTO 更新DTO
     * @return 处理结果
     */
    public Result<Boolean> handleUpdateRepository(String id, RepositoryCreateDTO updateDTO) {
        return executeOperation(
            () -> repositoryService.updateRepository(id, updateDTO),
            "更新仓库"
        );
    }

    /**
     * 处理删除仓库请求
     * 
     * @param id 仓库ID
     * @return 处理结果
     */
    public Result<Boolean> handleDeleteRepository(String id) {
        return executeOperation(
            () -> repositoryService.deleteRepository(id),
            "删除仓库"
        );
    }

    /**
     * 处理Fork仓库请求
     * 
     * @param sourceRepositoryId 源仓库ID
     * @param name 新仓库名称
     * @return 处理结果
     */
    public Result<RepositoryVO> handleForkRepository(String sourceRepositoryId, String name) {
        return executeOperation(
            () -> repositoryService.forkRepository(sourceRepositoryId, name),
            "Fork仓库"
        );
    }

    /**
     * 处理获取用户仓库列表请求
     * 
     * @param userId 用户ID
     * @param type 仓库类型
     * @return 处理结果
     */
    public Result<List<RepositoryVO>> handleGetUserRepositories(String userId, Integer type) {
        return executeOperation(
            () -> repositoryService.getUserRepositories(userId, type),
            "获取用户仓库列表"
        );
    }

    /**
     * 处理获取仓库统计信息请求
     * 
     * @param ownerId 所有者ID
     * @return 处理结果
     */
    public Result<RepositoryStatisticsDTO> handleGetRepositoryStatistics(String ownerId) {
        return executeOperation(
            () -> repositoryService.getRepositoryStatistics(ownerId),
            "获取仓库统计信息"
        );
    }

    /**
     * 处理归档仓库请求
     * 
     * @param id 仓库ID
     * @return 处理结果
     */
    public Result<Boolean> handleArchiveRepository(String id) {
        return executeOperation(
            () -> repositoryService.archiveRepository(id),
            "归档仓库"
        );
    }

    /**
     * 处理取消归档仓库请求
     * 
     * @param id 仓库ID
     * @return 处理结果
     */
    public Result<Boolean> handleUnarchiveRepository(String id) {
        return executeOperation(
            () -> repositoryService.unarchiveRepository(id),
            "取消归档仓库"
        );
    }
}
